<?php

namespace app\site\controller\store;

use app\common\exception\MiException;
use app\model\store\GoodsCate;
use app\site\controller\AController;
use app\site\service\SiteAuthService;
use app\uniapp\service\GoodsCateService;
use app\uniapp\service\GoodsService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Request;

class UniShowController extends AController
{


    private mixed $siteId;

    /**
     * @throws MiException
     */
    protected function initialize()
    {
        $this->siteId = SiteAuthService::instance()->getSiteId();
    }

    public function goodsLimit(Request $request): \think\response\Json
    {
        $limit = $request->param('limit', 10);
        $cate_id = $request->param('cate_id', 0);
        $tag_id = $request->param('tag_id',0);
        $where = [];
        if ($cate_id!=0){
            $cateSer = GoodsCateService::instance();
            $cates = $cateSer->select();
            $cateIds = $cateSer->childrenIds($cates, $cate_id);
            // 追加当前分类
            $cateIds[] = $cate_id;
            $where['cateIds'] = $cateIds;
        }
        if ($tag_id != 0){
            $where['tagId'] = $tag_id;
        }

        $goodsSer = GoodsService::instance();
        $list = $goodsSer->list($limit, $where);

        return jsonSuccess([
            'items' => $list
        ]);
    }


    /** 获取前端分类列表
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     */
    public function goodsCateList(): \think\response\Json
    {
        $pid = $this->param("pid", 0);
        $list = GoodsCate::where('show_type',1)->where('site_id', $this->siteId)->where('pid',$pid)->select();

        return jsonSuccess([
            'items' => $list,
        ]);
    }


}
